package com.ruoyi.web.controller.order;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.order.domain.OrderInfo;
import com.ruoyi.order.domain.dto.OrderInfoAppPageDTO;
import com.ruoyi.order.domain.dto.OrderInfoDTO;
import com.ruoyi.order.domain.vo.OrderInfoAppVO;
import com.ruoyi.order.service.IOrderInfoService;
import com.ruoyi.user.domain.AppUserAccount;
import com.ruoyi.user.service.IAppUserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * APP订单Controller
 *
 * @author ghostmen
 * @date 2024-11-03
 */
@Validated
@RestController
@RequestMapping("/app/orderInfo")
@Api(value = "APP用户订单", tags = "APP用户订单接口")
public class AppOrderInfoController extends BaseController {

    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private IAppUserAccountService appUserAccountService;

    /**
     * 查询订单列表
     */
    @GetMapping("/getOrderList")
    @ApiOperation(value = "查询订单列表", notes = "传入pageDTO")
    public TableDataInfo getOrderList(OrderInfoAppPageDTO pageDTO) {
        pageDTO.setUserId(getLoginAppUser().getAppUser().getUserId());
        startPage();
        PageInfo<OrderInfoAppVO> list = orderInfoService.getOrderList(pageDTO);
        return getDataTable(list);
    }


    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{orderNum}")
    @ApiOperation(value = "获取订单详细信息", notes = "传入orderNum")
    public AjaxResult getInfoByOrderId(@PathVariable("orderNum") String orderNum) {
        return success(orderInfoService.getInfoByOrderNum(orderNum));
    }

    /**
     * 用户游戏账号下拉列表
     */
    @ApiOperation(value = "用户游戏账号下拉列表")
    @GetMapping("/userAccountList")
    public TableDataInfo userAccountList() {
        List<AppUserAccount> list = appUserAccountService.userAccountList(getLoginAppUser().getUserId());
        return getDataTable(list);
    }


    /**
     * 下单
     */
    @RepeatSubmit
    @PostMapping("/buyOrder")
    @ApiOperation(value = "下单", notes = "传入orderInfoDTO")
    public AjaxResult buyOrder(@Valid @RequestBody OrderInfoDTO orderInfoDTO) {
        return successOrder(orderInfoService.buyOrder(orderInfoDTO, getLoginAppUser()));
    }

    /**
     * 小程序微信支付的第一步,统一下单
     *
     * @return
     * @author lantian
     * @time 2019年4月23日17:36:35
     */
    @RepeatSubmit
    @GetMapping("/payOrder")
    @ApiOperation(value = "支付", notes = "传入orderNum")
    public AjaxResult payOrder(@ApiParam(value = "订单编号", required = true) @NotBlank(message = "订单编号不能为空") @RequestParam String orderNum) {
        String ip = IpUtils.getIpAddr();
        return success(orderInfoService.payOrder(orderNum, ip, getLoginAppUser().getAppUser()));
    }

    /**
     * 取消订单
     */
    @GetMapping("/cancelOrder")
    @ApiOperation(value = "取消订单")
    public AjaxResult cancelOrder(@ApiParam(value = "订单编号", required = true) @RequestParam String orderNum) {
        return toAjax(orderInfoService.cancelOrder(getLoginAppUser(), orderNum));
    }

    /**
     * 用户删除订单
     */
    @DeleteMapping("/{orderIds}")
    @ApiOperation(value = "用户删除订单", notes = "传入serverTask")
    public AjaxResult userRemoveByIds(@ApiParam(value = "订单id", required = true) @PathVariable Long[] orderIds) {
        return toAjax(orderInfoService.userRemoveByIds(getLoginAppUser(), orderIds));
    }
}
